package ru.xeden.web.html.dom;

import java.io.PrintWriter;

import ru.xeden.web.html.RenderContext;


/**
 * Элемент для построения элементов DOM дерева при парсинге
 * @author evga
 *
 */
public final class DomMarkUpElement
    extends Element
{
    
    private final String name;
    
    public DomMarkUpElement(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }
    
    public void toMarkup(RenderContext markupContext, PrintWriter writer)
    {
        StringBuilder builder = new StringBuilder();
        builder.append("<").append(getName());
        for (Attribute attr = getFirstAttribute(); attr != null; attr = attr.getNextAttribute())
        {
            attr.render(markupContext, builder);
        }
        boolean hasChildren = hasChildren();
        String close = !hasChildren ? "/>" : ">";
        builder.append(close);
        writer.print(builder.toString());
        if (hasChildren)
        {
            writeChildMarkup(markupContext, writer);
        }
        if (hasChildren)
        {
            writer.print("</");
            writer.print(getName());
            writer.print(">");
        }
    }

}
